package com.back.controller;

import java.util.Base64;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.back.model.Doorplate;
import com.back.model.UploadResourceChunk;
import com.back.model.UploadResourceInformation;
import com.back.utils.Log;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @PostMapping("/initializeTheRepository")
    public String initializeTheRepository(@RequestBody Long userId) {
        try {
            Thread.sleep(1000); // 延时1秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.info(getClass(), "接收到用户ID: {}", userId);
        return "123123" + userId;
    }

    @PostMapping("/heartBeats")
    public Boolean heartBeats(@RequestBody Doorplate doorplate) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
         Log.info(getClass(), "心跳：{}/{}", doorplate.getUserId(), doorplate.getSecondaryPath());
        return true;
    }

    @PostMapping("/uploadChunk")
    public Boolean uploadChunk(@RequestBody UploadResourceChunk chunk) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        byte[] decode = Base64.getDecoder().decode(chunk.getChunk());
        Log.info(getClass(), "接收到Chunk：{}" ,chunk.getChunkIndex());
        return true;
    }

    @PostMapping("/splicingChunks")
    public Boolean splicingChunks(@RequestBody UploadResourceInformation info) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.info(getClass(), "合并：{}/{}", info.getUserId(), info.getSecondaryPath());
        return true;
    }

    @PostMapping("/cancelUpload")
    public Boolean cancelUpload(@RequestBody Doorplate doorplate) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.info(getClass(), "取消上传：{}/{}", doorplate.getUserId(), doorplate.getSecondaryPath());
        return true;
    }
    
}
